package com.iss.apartmentmanage.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sun.xml.internal.ws.spi.db.DatabindingException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyJson {
    public static String getJson(Object object){

        return  getJson(object,"yyyy-MM-dd HH:mm:ss");
    }
    public static String getJson(Object object,String dateFormat){

        //默认的时间是时间戳，转换成通用格式
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat stf = new SimpleDateFormat(dateFormat);
        objectMapper.setDateFormat(stf);
        try {
           return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }
}
